package com.chinacoal.microservice.util;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.io.Serializable;

/**
 * @author zhuliang
 * @version V1.0
 * @Description: 返回值管理类
 * @date 2020-11-06 14：30
 */
@Getter
public class Result<T> implements Serializable {

    @ApiModelProperty(value = "状态码，10000为成功")
    private int code;

    @ApiModelProperty(value = "提示信息")
    private String msg;

    @ApiModelProperty(value = "数据")
    private T data;

    @ApiModelProperty(value = "总记录数")
    private long total;

    public Result(T data,long total){
        this.code = 10000;
        this.msg = "success";
        this.data = data;
        this.total = total;
    }

    public Result(){
        Result.success(null);
    }

    //成功调用的构造方法
    public Result(T data){
        this.code = 10000;
        this.msg = "success";
        this.data = data;
    }

    //失败调用的构造方法
    public Result(CodeMsg msg) {
        if (null == msg) {
            return;
        }
        this.code = msg.getCode();
        this.msg = msg.getMsg();
    }

    //失败需要返回参数的构造方法
    public Result(CodeMsg msg, T data) {
        if (null == msg) {
            return;
        }
        this.code = msg.getCode();
        this.msg = msg.getMsg();
        this.data = data;
    }

    /**
     * @author zhuliang
     * @Description: 成功调用
     * @date 2020-11-06 14：30
     */
    public static <T> Result<T> success(T data){
        return new Result<T>(data);
    }

    /**
     * @author zhuliang
     * @Description: 成功调用（重载）
     * @date 2020-11-06 14：30
     */
    public static <T> Result<T> success(){
        return new Result<T>(CodeMsg.Success);
    }

    public static <T> Result<T> success(T data, long total){
        return new Result<T>(data,total);
    }

    public static <T> Result<T> success(CodeMsg msg, T data){
        return new Result<T>(msg, data);
    }

    /**
     * @author zhuliang
     * @Description: 失败调用
     * @date 2020-11-06 14：30
     */
    public static <T> Result<T> error(CodeMsg msg){
        return new Result<T>(msg);
    }

    /**
     * @author zhuliang
     * @Description: 失败调用（重载）
     * @date 2020-11-06 14：30
     */
    public static <T> Result<T> error(CodeMsg msg, T data){
        return new Result<T>(msg,data);
    }
}
